<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2023/10/11
 * Time: 17:54
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Config;
use think\Db;
use app\admin\model\Auth;
use app\admin\model\AliLocal;
use app\admin\model\Lianlians;
use app\admin\model\Index as IndexModel;
class Goods extends api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function goodszc(){
        $datainfo = IndexModel::setDecryptArray(Config::get("miyao.shi"),input());
        $datainfo = json_decode($datainfo,true);
        $item_id =  $datainfo['item_id'];
        $goods = Db::name('goods')->where('item_id',$item_id)->value('content');
        $this->success("获取成功",$goods);
    }
    /**
     * 商品详情
     *
     * @ApiMethod (POST)
     * @param string $item_id 商品ID
     * @param string $city 定位城市
     * @param string $type 商品类型[1、阿里 2、自建]
     */
    public function goodsInfo(){
        $user_id =$this->auth->id;
        $userInfo = Auth::userInfo($user_id);
        if($userInfo['vip_id'] == 0){
            $sfvip = Db::name('sanfang_vip')->where('end_time','>',time())->where('is_att',3)->where('is_t',0)->where('mobile',$userInfo['mobile'])->value('vip_id');
            if($sfvip){
                $userInfo['vip_id'] =$sfvip;
            }
        }
        $type = input("type",1);
        $item_id = input("item_id");
        $city = input("city",'');
        if(empty($item_id)){
            $this->error("商品ID不能为空");
        }
        $map['item_id'] = $item_id;
        $map['switch'] = 1;
        $info = Db::name("goods")->where($map)->find();
        if(!$info){
//            $this->error("今日已售罄,请明日再来");
            exit(json_encode(array('code' => 3, 'msg' => '今日已售罄，请明日再来'), JSON_UNESCAPED_UNICODE));
        }
        if($type==1){
        }else if($type==8 || $type==9){
            //橙券直充
            $map['item_id'] = $item_id;
            $map['switch'] = 1;
            $info = Db::name("goods")->where($map)->find();
            if(!empty($info)){
                $info['images'] = explode(',',$info['images']);
                if(!empty($info['images'])){
                    //$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
                    foreach($info['images'] as $k=>$vo){
                        $info['images'][$k] = [
                            'name'=>'',
                            'url'=>$vo//$http_type.$_SERVER['HTTP_HOST'].
                        ];
                    }
                }
                $brand = $info['brand'];
                $info['brand'] = [];
                $info['brand'] = [
                    'brand_id'=>time(),//随便模拟的brand_id,自建商品暂时没有brand_id
                    'brand_name'=>$brand
                ];
                $info['discount'] = $info['commission_rate'];
            }
            $info['xiao'] = round($info['activity_price_cent']/100,2);
            $info['da'] = round($info['original_price_cent']/100,2);
            if($userInfo['vip_id']<=0){
                $info['price'] =  round($info['original_price_cent']/100,2);
            }else{
                $info['price'] =  round($info['activity_price_cent']/100,2);
            }
            $this->success("获取成功",[
                'goods_info'=>$info
            ]);
        }else if($type==11){
            //爱吃商品
            $gg_id = input("gg_id");
            $map['switch'] = 1;
            $info = Db::name("goods")->where($map)->find();
            $info['apply_shop_count'] =   Db::name("acgoods_city")->where('item_id',$item_id)->count();
            if($gg_id){
                $info['acgoods'] = Db::name('acgoods_sku')->group('sku_id')->where('item_id',$map['item_id'])->where('sku_id',$gg_id)->select();
            }else{
                $info['acgoods'] = Db::name('acgoods_sku')->group('sku_id')->where('item_id',$map['item_id'])->select();
            }
            $info['images'] =json_decode( $info['images'],true);
            $info['content'] = json_decode($info['content'],true);
            foreach ($info['acgoods'] as $k=>&$v){
                if($userInfo['vip_id']<=0){
                    $v['sku_activity_price_cent'] = round($v['sku_original_price_cent']/100,2);
                }else{
                    $v['sku_activity_price_cent'] = round($v['sku_activity_price_cent']/100,2);
                }
            }
            $info['xiao'] = round($info['activity_price_cent']/100,2);
            $info['da'] = round($info['original_price_cent']/100,2);
            $this->success("获取成功",[
                'goods_info'=>$info
            ]);
        } else if($type==10){
            $gg_id = input("gg_id");
            //联联
            $map['item_id'] = $item_id;
            $map['switch'] = 1;
            $info = Db::name("goods")->where($map)->find();
            $info['apply_shop_count'] =   Db::name("ll_shoplist")->where('item_id',$item_id)->count();
            if($gg_id){
                $info['ll_goods'] = Db::name('ll_itemlist')->group('itemId')->where('item_id',$map['item_id'])->where('itemId',$gg_id)->select();
            }else{
                $info['ll_goods'] = Db::name('ll_itemlist')->group('itemId')->where('item_id',$map['item_id'])->select();
            }
            foreach ($info['ll_goods'] as $k=>&$v){
                $infos = new Lianlians;
                $res =  $infos->getProductDetails($item_id,$v['itemId']);
                $info['ll_goods'][$k]['info'] = $res['data']['encryptedData'];
                if($userInfo['vip_id']<=0){
                    $v['salePrice'] = round($v['originPrice']/100,2);
                }else{
                    $v['salePrice'] = round($v['salePrice']/100,2);
                }
            }
            $info['xiao'] = round($info['activity_price_cent']/100,2);
            $info['da'] = round($info['original_price_cent']/100,2);
            $this->success("获取成功",[
                'goods_info'=>$info
            ]);
        }else if($type==2){
            //自建商品
            $map['item_id'] = $item_id;
            $map['switch'] = 1;
            $info = Db::name("goods")->where($map)->find();
            if(!empty($info)){
                $info['images'] = explode(',',$info['images']);
                if(!empty($info['images'])){
                    foreach($info['images'] as $k=>$vo){
                        $info['images'][$k] = [
                            'name'=>'',
                            'url'=>$vo//$http_type.$_SERVER['HTTP_HOST'].
                        ];
                    }
                }
                $brand = $info['brand'];
                $info['brand'] = [];
                $info['brand'] = [
                    'brand_id'=>time(),
                    'brand_name'=>$brand
                ];
                $info['discount'] = $info['commission_rate'];
                $info['ar_type'] = 1;
            }
            $info['xiao'] = round($info['activity_price_cent']/100,2);
            $info['da'] = round($info['original_price_cent']/100,2);
            if($userInfo['vip_id']<=0){
                $info['price'] =  round($info['original_price_cent']/100,2);
            }else{
                $info['price'] =  round($info['activity_price_cent']/100,2);
            }
            $this->success("获取成功",[
                'goods_info'=>$info
            ]);
        }
    }

    /**
     * 店铺列表
     *
     * @ApiMethod (POST)
     * @param string $item_id 商品ID
     * @param string $city 定位城市
     */
    public function shopList(){
        $item_id = input("item_id");
        $city = input("city",'');
        $type = input('type');
        $page = input('page',1);
        $page_size = input('page_size',20);
        if(empty($item_id)){
            $this->error("商品ID不能为空");
        }
        $maps=[];
        if($type){
            $maps['type']=$type;
        }
        $goodsInfo = Db::name("goods")->where(['item_id'=>$item_id])->where($maps)->find();
        $city_id = Db::name("ali_city")->where(['city_name'=>$city])->value('city_code');
        $shop_info = [];
        if(empty($goodsInfo)){
            $this->error("商品已售罄,请明日再来");
        }else{
            if($goodsInfo['type']==2){
                $mapd=[];
                //自建商品
                if($city_id){
                    $mapd['city'] =  $city_id;
                }
                $data = Db::name("goods_shop")->where($mapd)->where(['item_id'=>$item_id])->select();
                if(!empty($data)){
                    foreach($data as $k=>$v){
                        $info['basic_info']['name'] = $v['shop_name'];
                        $info['basic_info']['location']['address'] = $v['address'];
                        $info['basic_info']['location']['latitude'] = $v['latitude'];
                        $info['basic_info']['location']['longitude'] = $v['longitude'];
                        $shop_info[] = $info;
                    }
                }
            }else if($goodsInfo['type']==11){//爱吃商品
                $mapd=[];
                if($city_id){
                    $mapd['city'] =  $city_id;
                }
                $data = Db::name("acgoods_city")->page($page,$page_size)->where('item_id',$item_id)->select();
                if(!empty($data)){
                    foreach($data as $k=>$v){
                        $info['basic_info']['name'] = $v['store_name'];
                        $info['basic_info']['location']['latitude'] = $v['latitude'];
                        $info['basic_info']['location']['longitude'] = $v['longitude'];
                        $shop_info[] = $info;
                    }
                }
            }else if($goodsInfo['type']==10){
                $mapd=[];
                if($city_id){
                    $mapd['cityCode'] =  $city_id;
                }
                //联联
                $data = Db::name("ll_shoplist")->where('item_id',$item_id)->select();
                if(!empty($data)){
                    foreach($data as $k=>$v){
                        $info['basic_info']['name'] = $v['name'];
                        $info['basic_info']['location']['address'] = $v['address'];
                        $info['basic_info']['location']['latitude'] = $v['latitude'];
                        $info['basic_info']['location']['longitude'] = $v['longitude'];
                        $shop_info[] = $info;
                    }
                }
            }
        }
        $this->success("获取成功",$shop_info);
    }
}